package cn.crism.blog.util;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil implements Runnable {
    private String email;// 收件人邮箱
    private String code;// 激活码
    private String username;

    public MailUtil(String email, String code, String username) {
        this.email = email;
        this.code = code;
        this.username = username;
    }

    public void run() {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        String from = "123483115@qq.com";// 发件人电子邮箱
        String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.smtp.auth", "true");// 打开认证

        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("123483115@qq.com", "vxfsucfphtvjbiei"); // 发件人邮箱账号、授权码
                }
            });

            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3设置邮件主题
            message.setSubject("课程申请失败反馈：");
            // 2.4设置邮件内容
            String content = "<html><head><body style='margin: 0; padding: 0;'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr>"
                    + "<td style='padding: 10px 0 30px 0;'><table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border: 1px dashed #cccccc; border-collapse: collapse;'>"
                    + "<tr><td bgcolor='#ffffff' style='padding: 40px 30px 40px 30px;'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr>"
                    + "<td style='color: #153643; font-family: Arial, sans-serif; font-size: 24px;'>"
                    + "你好 " + username
                    + "</td>	</tr><tr><td style='padding: 30px 20px 20px 20px; color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 20px;'>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + code
                    + "</td></tr></table></td></tr><tr><td bgcolor='#ee4c50' style='padding: 30px 30px 30px 30px;'>"
                    + "	<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align='center' style='color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;' width='75%' >©2017 CrismCN Corporation. All rights reserved. 湘ICP备17011555号"
                    + "</td></tr></table></td></tr></table></td></tr></table></body></html>";
            message.setContent(content, "text/html;charset=UTF-8");
            // 3.发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
